﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.GsInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.GoodInfo;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.GoodInfo
{
    public class GsExtraInfoReponsitoty : SqlServerRespository<GsExtraInfoEntity>, IGsExtraInfoReponsitoty
    {
        public GsExtraInfoReponsitoty()
        {
            Context = new WechatMallContext();
        }

        public GsExtraInfoEntity GetGsExtraInfoEntityById(long gsid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsExtraInfoEntity>();
            var sql = $@"SELECT {querysql} FROM gsextrainfo 
            WHERE gsid=@gsid";
            var result = GetList(sql, new {gsid}).FirstOrDefault();
            return result;
        }

        public IEnumerable<GsExtraInfoEntity> GetAllgs()
        {
            return GetAll();
        }

        public IEnumerable<GsExtraInfoEntity> GetGsExtraInfoByShopId(long shopId)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsExtraInfoEntity>("a");
            var sql = $@"SELECT {querysql} FROM gsextrainfo a
            INNER JOIN shopgsrelationship b ON a.gsid=b.gsid
            WHERE b.shopid=@shopid";
            var result = GetList(sql, new {shopId});
            return result;
        }

        public IEnumerable<GsExtraInfoEntity> GetGsExtraInfoByGroupId(long groupid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsExtraInfoEntity>("a");
            var sql = $@"SELECT {querysql} FROM gsextrainfo a
             INNER JOIN groupgsrelationship b ON a.gsid=b.gsid
            WHERE b.groupid=@groupid";
            var result = GetList(sql, new {groupid});
            return result;
        }

        public IEnumerable<GsExtraInfoEntity> GetGsExtraInfoByAppId(string appid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsExtraInfoEntity>("a");
            var sql = $@"SELECT {querysql} FROM gsextrainfo a
             INNER JOIN appgsrelationship b ON a.gsid=b.gsid
            WHERE b.appid=@appid";
            var result = GetList(sql, new {appid});
            return result;
        }

        public void RemoveByGsid(long gsid, UnitOfWork unit = null)
        {
            DapperHelper.Execute($"delete from gsextrainfo where gsid=@gsid", Context, unit, new {gsid});
        }

        public void UpdateSellCount(UnitOfWork unit = null)
        {
            //todo 通过作业调用,订单事件待处理表
            DapperHelper.Execute("", Context, unit);
        }

        public void UpdateOldPrice(decimal oldprice, long gsid, UnitOfWork unit = null)
        {
            DapperHelper.Execute($"UPDATE gsextrainfo SET gsoldprice={oldprice} WHERE gsid={gsid}", Context, unit);
        }
    }
}